// index.js
var api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners: [{
      image: ''
    }],
    ads: [],
    host:api.host,
    userInfo:'',
    shop_lists: [],
    longitude: '',
    latitude: '',
    navs: [{
        name: "好友拼团",
        icon: "/images/m1.png"
      },
      {
        name: "猴省心购",
        icon: "/images/m2.png"
      },
      // {
      //   name: "领券中心",
      //   icon: "/images/m3.png"
      // }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('url',options);
    if(options.scene){
      this.binTid(options.scene)
    }
  },
  binTid(tid) {
    // 获取OPENID及用户是否已注册
    var that = this;

        wx.login({
          success: res => {
            api.post('/oauth/wechatLogin' ,{
              code:res.code,
              tid:tid
            }).then(res => {
              wx.setStorageSync('token', res.token);
              wx.setStorageSync('userInfo', res.userInfo);
            })
          }
        })

  },
  //去登录
  toLogin() {
    wx.navigateTo({
      url: '../login/login',
    })
  },
  toInvitationPoster() {
    wx.navigateTo({
      url: '/pages/invitation_poster/invitation_poster',
    })

  },
  //点击导航
  click_nav(e) {
    const index = e.currentTarget.dataset.index;
    if (index == 0) {
      wx.navigateTo({
        url: '../promotion_income/promotion_income',
      })
    }
    if (index == 1) {
      wx.switchTab({
        url: '/pages/shop/shop',
      })
    }
    if (index == 2) {
      wx.navigateTo({
        url: '../coupons/coupons',
      })
    }


  },
  //点击当前位置
  click_cur_position() {
    let that = this;
    wx.chooseLocation({
      success: res => {
        console.log('选择地址', res)
        wx.setStorageSync('Location', res);
        let my_address = that.data.my_address;
        my_address.address = res.name;
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude,
          my_address: my_address
        })
      }
    })
  },
  //快速预约
  click_make_appointment() {
    wx.navigateTo({
      url: "/pages/select_shop/select_shop"
    })
  },
  //我要加盟
  toAbout(){
    wx.navigateTo({
      url: '/pages/about_us/about_us',
    })
  },
  
  // 首页信息
  getCommonIndex() {
    api.get('/common/index').then(res => {
      this.setData({
        ads: res.items,
        banners: res.banners,
        articles: res.articles
      })
    })
  },
  adUrl(e){
    let type = e.currentTarget.dataset.jump;
    console.log('xx',type)
    if(type == "service"){
      wx.navigateTo({
        url: '/pages/select_shop/select_shop'
      })
    }else if(type =="shop"){
      wx.switchTab({
        url: '/pages/shop/shop',
      })
    }
  },
  toShop(){
    wx.switchTab({
      url: '/pages/shop/shop',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCommonIndex();
    // if (!this.data.latitude && !this.data.longitude) {
    //   this.checkSetting();
    // }
    this.setData({
      userInfo:wx.getStorageSync('userInfo')
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})